<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分析项目" prop="fxxm">
        <el-input v-model="queryParams.fxxm" placeholder="请输入分析项目" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="报告日期" prop="bgrq">
        <el-date-picker :editable="false" clearable size="small" v-model="queryParams.bgrq" type="date"
          value-format="yyyy-MM-dd" placeholder="选择报告日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['Laboratory_weight:weight:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['Laboratory_weight:weight:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['Laboratory_weight:weight:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['Laboratory_weight:weight:export']"
          >导出</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-s-check" size="mini" :disabled="multiple" @click="handleCheck"
          v-hasPermi="['Laboratory_weight:weight:check']">审核</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="weightList" @selection-change="handleSelectionChange" max-height="550">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" width="100px" />
      <el-table-column label="分析项目" align="center" prop="fxxm" />
      <!-- <el-table-column label="检测依据" align="center" prop="jcyj" />
      <el-table-column label="检测方法" align="center" prop="jcff" />
      <el-table-column label="试验环境-温度" align="center" prop="wd" />
      <el-table-column label="试验环境-湿度" align="center" prop="sd" /> -->
      <!-- <el-table-column label="检测日期" align="center" prop="jcrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jcrq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="报告日期" align="center" prop="bgrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bgrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="仪器名称" align="center" prop="yqmc" />
      <el-table-column label="仪器型号" align="center" prop="yqxh" />
      <!-- <el-table-column label="计算公式" align="center" prop="jsgs" /> -->
      <el-table-column label="报表编号" align="center" prop="bbbh" min-width="140" />
      <el-table-column label="审核人" align="center" prop="shrPerson" />
      <el-table-column label="检验员" align="center" prop="jyy" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="250px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="excelhandleView(scope.row)">导出</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['Laboratory_weight:weight:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['Laboratory_weight:weight:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改重量法检验原始记录对话框 -->
    <el-dialog fullscreen v-myFullscreen :title="title" :visible.sync="open" width="900px" append-to-body
      custom-class="myDialog">
      <TableHeader @nameChange="nameChange" :data="{
          mainTitle: '重量法检验原始记录',
          company: '青岛崂山矿泉水有限公司',
          code: form.bbbh,
        }" />
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="分析项目" prop="fxxm">
              <el-input v-model="form.fxxm" placeholder="请输入分析项目" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测依据" prop="jcyj">
              <el-input v-model="form.jcyj" placeholder="请输入检测依据" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测方法" prop="jcff">
              <el-input v-model="form.jcff" placeholder="请输入检测方法" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-row>
              <el-col :span="14">
                <el-form-item label="试验环境:温度℃" prop="wd">
                  <el-input v-model="form.wd" placeholder="请输入温度" :disabled="openDisabled" />
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="湿度%" prop="sd" label-width="60px">
                  <el-input v-model="form.sd" placeholder="请输入湿度" :disabled="openDisabled" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测日期" prop="jcrq">
              <el-date-picker :editable="false" clearable v-model="form.jcrq" type="date" value-format="yyyy-MM-dd"
                placeholder="选择检测日期" :disabled="openDisabled">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报告日期" prop="bgrq">
              <el-date-picker :editable="false" clearable v-model="form.bgrq" type="date" value-format="yyyy-MM-dd"
                placeholder="选择报告日期" :disabled="openDisabled">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仪器名称" prop="yqmc">
              <el-input v-model="form.yqmc" placeholder="请输入仪器名称" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仪器型号" prop="yqxh">
              <el-input v-model="form.yqxh" placeholder="请输入仪器型号" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="计算公式" prop="jsgs">
              <el-input v-model="form.jsgs" placeholder="请输入计算公式" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
                <el-form-item label="报表编号" prop="bbbh">
                  <el-input v-model="form.bbbh" placeholder="请输入报表编号" :disabled="openDisabled" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="审核人" prop="shrPerson">
                  <el-input v-model="form.shrPerson" placeholder="请输入审核人" :disabled="openDisabled" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="检验员" prop="jyy">
                  <el-input v-model="form.jyy" placeholder="请输入检验员" :disabled="openDisabled" />
                </el-form-item>
            </el-col> -->
          <!-- <el-col :span="12">
                <el-form-item label="备注" prop="remark">
                  <el-input v-model="form.remark" placeholder="请输入备注" :disabled="openDisabled" />
                </el-form-item>
            </el-col> -->
        </el-row>
        <br />
        <el-divider content-position="center">重量法检验原始记录子信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddQualityLabWeightSub"
              :disabled="openDisabled">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteQualityLabWeightSub"
              :disabled="openDisabled">删除</el-button>
          </el-col>
        </el-row>
        <el-divider content-position="center"></el-divider>
        <el-table :data="qualityLabWeightSubList" :row-class-name="rowQualityLabWeightSubIndex" border
          @selection-change="handleQualityLabWeightSubSelectionChange" ref="qualityLabWeightSub">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" v-if="false" />
          <el-table-column label="样品名称" prop="ypmc">
            <template slot-scope="scope">
              <!-- <el-input
                v-model="scope.row.ypmc"
                placeholder="请输入样品名称"
                :disabled="openDisabled"
              /> -->
              <Dialog @dialogChange="dialogChange($event, scope)" :echoValue="scope.row.ypmc" parameter="rows"
                value="id" label="name" :multiple="false" search="name/code" url="/basic/productInfo/listSelect"
                :column="['产成品编号/code', '产成品名称/name']" placeholder="请选择内容" :axiosData="{ typeCode: 'YYTRKQS' }"
                :openDisabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="生产批次/取样日期" prop="scpc" width="160">
            <template slot-scope="scope">
              <el-input v-model="scope.row.scpc" @input="scpcChange(scope.$index)" placeholder="请输入生产批次/取样日期"
                :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="取样量V(mL)" prop="qyl" width="110">
            <template slot-scope="scope">
              <el-input @input="caculate" v-model="scope.row.qyl" placeholder="请输入取样量V(mL)" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="空皿称量m1(g) 首次" prop="kmcl1" width="150">
            <template slot-scope="scope">
              <el-input @input="caculate" v-model="scope.row.kmcl1" placeholder="请输入空皿称量m(g) 首次" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="空皿称量m2(g) 重复" prop="kmcl2" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.kmcl2" placeholder="请输入空皿称量m(g) 重复" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="空皿+待测物称量m3(g) 首次" prop="kmclsum" width="200">
            <template slot-scope="scope">
              <el-input @input="caculate" v-model="scope.row.kmclsum" placeholder="请输入空皿+待测物称量m1(g) 首次" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="空皿+待测物称量m4(g) 重复" prop="kmdcw" width="200">
            <template slot-scope="scope">
              <el-input v-model="scope.row.kmdcw" placeholder="请输入空皿+待测物称量m1(g) 重复" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="待测物m0" prop="dcw">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dcw" placeholder="请输入待测物m0" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="样品结果" prop="ypjg">
            <template slot-scope="scope">
              <el-input v-model="scope.row.ypjg" placeholder="请输入样品结果" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <!-- <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
                <el-input v-model="scope.row.remark" placeholder="请输入备注" :disabled="openDisabled"/>
            </template>
          </el-table-column> -->
        </el-table>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="审核人" prop="shrPerson">
              <el-input v-model="form.shrPerson" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验员" prop="jyy">
              <el-select v-model="form.jyy" filterable allow-create placeholder="请选择检验员" style="width: 100%"
                :disabled="openDisabled">
                <el-option v-for="item in userList" :key="item.nickName" :label="item.nickName" :value="item.nickName"
                  :disabled="openDisabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listWeight,
    getWeight,
    delWeight,
    addWeight,
    updateWeight,
    checkWeight,
    getCapResult
  } from "@/api/quality/laboratory/weight";
  import { getInfo } from "@/api/login";
  import { listUser } from "@/api/system/user";

  export default {
    name: "Weight",
    data() {
      return {
        //弹窗是否禁止编辑
        openDisabled: true,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 子表选中数据
        checkedQualityLabWeightSub: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 重量法检验原始记录表格数据
        weightList: [],
        // 重量法检验原始记录子表格数据
        qualityLabWeightSubList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 50,
          fxxm: null,
          bgrq: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        /* 当前用户名 */
        nickName: "",
        userList: [],
      };
    },
    created() {
      this.getList();
      this.queryUserList();
    },
    methods: {
      /** 查询重量法检验原始记录列表 */
      getList() {
        this.loading = true;
        listWeight(this.queryParams).then((response) => {
          this.weightList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          fxxm: null,
          jcyj: null,
          jcff: null,
          wd: null,
          sd: null,
          jcrq: null,
          bgrq: null,
          yqmc: null,
          yqxh: null,
          jsgs: "溶解性总固体：ρ=(m4-m2)*1000*1000/V+1/2ρ（HCO3-）",
          bbbh: "LK-QF-LS1-10-012-11-01",
          shrPerson: null,
          jyy: this.nickName,
          remark: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          delFlag: null,
        };
        this.qualityLabWeightSubList = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.id);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.openDisabled = false;
        this.open = true;
        this.title = "添加重量法检验原始记录";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.openDisabled = false;
        const id = row.id || this.ids;
        getWeight(id).then((response) => {
          this.form = response.data;
          this.qualityLabWeightSubList = response.data.qualityLabWeightSubList;
          console.log(this.qualityLabWeightSubList)
          for (let item of this.qualityLabWeightSubList) {
            getCapResult({ ypmc: item.ypmc, scpc: item.scpc }).then((response) => {
              if (response.code == 200) {
                item.temporary = Number(response.data)
              } else {
                item.temporary = 0
              }
            });
          }

          this.open = true;
          this.title = "修改重量法检验原始记录";
        });
      },
      /** 查看按钮 */
      handleView(row) {
        this.reset();
        this.openDisabled = true;
        const id = row.id || this.ids;
        getWeight(id).then((response) => {
          this.form = response.data;
          this.qualityLabWeightSubList = response.data.qualityLabWeightSubList;
          this.open = true;
          this.title = "查看重量法检验原始记录";
        });
      },
      dialogChange(event, scope) {
        this.qualityLabWeightSubList[scope.$index].ypmc = event.label;
        getCapResult({ ypmc: this.qualityLabWeightSubList[scope.$index].ypmc, scpc: this.qualityLabWeightSubList[scope.$index].scpc }).then((response) => {
          if (response.code == 200) {
            this.qualityLabWeightSubList[scope.$index].temporary = Number(response.data)
          } else {
            this.qualityLabWeightSubList[scope.$index].temporary = 0
          }
          this.caculate()
        });
     
      },
      scpcChange(index) {
        getCapResult({ ypmc: this.qualityLabWeightSubList[index].ypmc, scpc: this.qualityLabWeightSubList[index].scpc }).then((response) => {
          if (response.code == 200) {
            this.qualityLabWeightSubList[index].temporary = Number(response.data)
          } else {
            this.qualityLabWeightSubList[index].temporary = 0
          }
          this.caculate()
        });
       
      },

      //标液浓度变化
      caculate() {
        for (let item of this.qualityLabWeightSubList) {
          item.ypjg = (((item.kmdcw - item.kmcl2) * 1000*1000 / item.qyl) + (0.5 * item.temporary)).toFixed(2)
        }
        this.$forceUpdate()
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            this.form.qualityLabWeightSubList = this.qualityLabWeightSubList;
            /**打开遮罩层*/
            const rLoading = this.openLoading();
            if (this.form.id != null) {
              updateWeight(this.form)
                .then((response) => {
                  /**成功后执行关闭遮罩层*/
                  rLoading.close();
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                })
                .catch((error) => {
                  /**请求异常后执行关闭遮罩层*/
                  rLoading.close();
                });
            } else {
              addWeight(this.form)
                .then((response) => {
                  /**成功后执行关闭遮罩层*/
                  rLoading.close();
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                })
                .catch((error) => {
                  /**请求异常后执行关闭遮罩层*/
                  rLoading.close();
                });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm(
          '是否确认删除重量法检验原始记录编号为"' + ids + '"的数据项?',
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return delWeight(ids);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          })
          .catch(() => { });
      },
      /** 重量法检验原始记录子序号 */
      rowQualityLabWeightSubIndex({ row, rowIndex }) {
        row.index = rowIndex + 1;
      },
      /** 重量法检验原始记录子添加按钮操作 */
      handleAddQualityLabWeightSub() {
        let obj = {};
        obj.ypmc = "";
        obj.scpc = "";
        obj.qyl = "";
        obj.kmcl1 = "";
        obj.kmcl2 = "";
        obj.kmclsum = "";
        obj.dcw = "";
        obj.ypjg = "";
        obj.kmdcw = "";
        obj.remark = "";
        this.qualityLabWeightSubList.push(obj);
      },
      /** 重量法检验原始记录子删除按钮操作 */
      handleDeleteQualityLabWeightSub() {
        if (this.checkedQualityLabWeightSub.length == 0) {
          this.$alert("请先选择要删除的重量法检验原始记录子数据", "提示", {
            confirmButtonText: "确定",
          });
        } else {
          this.qualityLabWeightSubList.splice(
            this.checkedQualityLabWeightSub[0].index - 1,
            1
          );
        }
      },
      /** 单选框选中数据 */
      handleQualityLabWeightSubSelectionChange(selection) {
        if (selection.length > 1) {
          this.$refs.qualityLabWeightSub.clearSelection();
          this.$refs.qualityLabWeightSub.toggleRowSelection(selection.pop());
        } else {
          this.checkedQualityLabWeightSub = selection;
        }
      },
      excelhandleView(row) {
        this.download(
          "Laboratory/weight/exportexcel?id=" + row.id,
          {
            ...this.queryParams,
          },
          "重量法检验原始记录" + this.parseTime(new Date()) + ".xlsx"
        );
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download(
          "Laboratory_weight/weight/export",
          {
            ...this.queryParams,
          },
          `Laboratory_weight_weight.xlsx`
        );
      },
      //接收编号
      nameChange(e) {
        this.form.bbbh = e;
      },
      /** 审核按钮操作 */
      handleCheck(row) {
        /* 审核 */
        this.ids.forEach((id) => {
          getWeight(id).then((response) => {
            if (
              response.data.shrPerson != null &&
              response.data.shrPerson != ""
            ) {
              this.msgError(
                '编号为"' + id + '"的数据项已审核完成，无需重复审核！'
              );
            } else {
              let obj = {};
              obj.id = id;
              obj.shrPerson = this.nickName;
              obj.qualityLabWeightSubList = this.qualityLabWeightSubList;
              /* 修改 */
              checkWeight(obj).then((response) => {
                this.msgSuccess('编号为"' + id + '"的数据项审核成功！');
                this.getList();
              });
            }
          });
        });
      },
      /* 查询用户列表 */
      queryUserList() {
        listUser().then((response) => {
          this.userList = response.rows;
        });
        getInfo().then((response) => {
          this.nickName = response.user.nickName;
        });
      },
    },
  };
</script>